package com.zide.auth.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zide.auth.domain.entity.member.MemberUser;
import com.zide.auth.domain.vo.request.LoginReqVO;
import com.zide.auth.mapper.member.UmsMemberMapper;
import com.zide.auth.service.MemberUserService;
import com.zide.auth.constant.UserConstants;
import com.zide.auth.utils.ZiDeContext;
import com.zide.auth.utils.satoken.StpMemberUtil;
import com.zide.common.enums.UserStatusEnum;
import com.zide.common.exception.ServiceException;
import com.zide.common.utils.NicknameUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author 石添
 * @date 2024/8/24
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MemberUserServiceImpl implements MemberUserService {

    private final UmsMemberMapper memberMapper;
    /**
     * 登录
     */
    @Override
    public String login(LoginReqVO reqVO) {

        String account = reqVO.getAccount();
        LambdaQueryWrapper<MemberUser> wrapper = new LambdaQueryWrapper<MemberUser>().eq(MemberUser::getMobile, account);
        MemberUser dbBean = memberMapper.selectOne(wrapper);
        log.info("loadUserByUsername=====>{}",dbBean);
        if (ObjectUtil.isNull(dbBean)) {
            log.info("登录用户：{} 不存在.", account);
            // 用户不存在自动注册登录
            dbBean = registerMember(account);
        }  else if (UserStatusEnum.DISABLE.getCode().equals(dbBean.getStatus())) {
            log.info("登录用户：{} 已被停用.", account);
            throw new ServiceException("用户被锁定");
        }
        // 判断验证码
        if(!StrUtil.equals(reqVO.getCode(),"888888")) {
            throw new ServiceException("验证码错误");
        }
        StpMemberUtil.login(dbBean.getId());
        SaTokenInfo tokenInfo = StpMemberUtil.getTokenInfo();
        return tokenInfo.getTokenValue();
    }

    /**
     * 退出登录
     */
    @Override
    public void logout() {
        Long userId = ZiDeContext.getMemberUserId();
        if(ObjectUtil.isNull(userId)) {
            throw new ServiceException("无效登录信息");
        }
        StpMemberUtil.logout(userId);
    }

    /**
     * 获取用户所有角色和权限信息
     */
    @Override
    public String searchPermission() {
        // TODO 获取用户所有角色和权限信息
        return null;
    }

    /**
     * 注册并登录
     */
    private MemberUser registerMember(String account) {
        MemberUser insertBean = new MemberUser();
        insertBean.setAvatar(UserConstants.DEFAULT_AVATAR);
        insertBean.setStatus(UserConstants.NORMAL);
        insertBean.setMobile(account);
        insertBean.setDescription(UserConstants.DESCRIPTION);
        insertBean.setNickname(NicknameUtil.getName());
        insertBean.setIntegral(0);
        insertBean.setDeleted(0);
        memberMapper.insert(insertBean);
        log.info("添加的用户==》{}",insertBean);
        return insertBean;
    }
}
